/*
 * @Author: your name
 * @Date: 2021-06-19 09:52:57
 * @LastEditTime: 2021-11-08 17:23:23
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /shujucaiji/app.js
 */
//引入依赖关系
const express = require('express');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
// const jwtCheck = require('express-jwt');

// 配置文件
// const CONFIG = require('./config');

//实例化express
const app = express();

// // 路由
const { submit, project, operator, summary, statistic, record } = require('./lib/routes');

//允许跨域
const cors = require('cors');
app.use(cors());

//关在express中间件,要放在路由之前
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

// 使用JWT验证Token，排除一些不需要验证的路径
// app.use(jwtCheck({secret:CONFIG.JWT_SECRET}).unless({path:['/wx','/be/token','/be/actoken']}));

// 公开文件
// app.use(express.static('public'), (req, res) => {
//   console.log('静态文件');
//   console.log(req.cookies, '---');
//   const { token } = req.cookies;
//   if(!token) {
//     res.send('000');
//   }
// });

//路径路由，放在最后
app.use('/submit', submit);
app.use('/project', project);
app.use('/operator', operator);
app.use('/summary', summary);
app.use('/statistic', statistic);
app.use('/record', record);

//捕获404错误
app.use((req, res, next)=>{
  let err = new Error('Not Found');
  err.status = 404;
  next(err);
});

//错误处理
app.use((err, req, res)=>{
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // 返回错误消息
  res.status(err.status || 500);
  res.json({err:err.status,name:err.name});
});


module.exports = app;